from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout
import sys
from PySide2.QtCharts import QtCharts
from PySide2.QtGui import QPainter, QPen, QColor
from PySide2.QtCore import Qt
import openpyxl

class ShowPage(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(1000, 400)
        self.chart = QtCharts.QChart()  # 图
        self.chartview = QtCharts.QChartView(self.chart) # 图层
        self.series = QtCharts.QSplineSeries()  #数据
        self.series2 = QtCharts.QSplineSeries()  # 数据
        self.series3 = QtCharts.QSplineSeries()  # 数据
        self.axisX = QtCharts.QValueAxis()      #坐标
        self.axisY = QtCharts.QValueAxis()
        self.axisY2 = QtCharts.QValueAxis()

        self.wb = openpyxl.load_workbook('C:\\Users\\zuozh\\Desktop\\太原科大Python综合培训\\国家数据\\人口\\总人口.xlsx')
        self.ws = self.wb['年度数据']
        self.buildChart()

    def buildChart(self):
        self.axisX.setRange(1999, 2018)         # 设置范围
        self.axisX.setLabelFormat('%u')         # 刻度格式
        self.axisX.setGridLineVisible(True)     # 网络线可见
        self.axisX.setTickCount(20)             # 设置大格数量
        self.axisX.setMinorTickCount(1)         # 每个大格子里刻度数量
        self.axisX.setTitleText('年份')         # 描述

        self.axisY.setRange(125000, 140000)
        self.axisY.setLabelFormat('%u')
        self.axisY.setGridLineVisible(True)
        self.axisY.setTickCount(10)
        self.axisY.setMinorTickCount(1)
        self.axisY.setTitleText('总人口（万人）')
        self.axisY2.setRange(61000, 71500)
        self.axisY2.setLabelFormat('%u')
        self.axisY2.setGridLineVisible(True)
        self.axisY2.setTickCount(10)
        self.axisY2.setMinorTickCount(1)
        self.axisY2.setTitleText('男/女性人口（万人）')

        self.chart.addAxis(self.axisX, Qt.AlignBottom)  # 添加坐标，靠下
        self.chart.addAxis(self.axisY, Qt.AlignLeft)    # 添加坐标，靠左
        self.chart.addAxis(self.axisY2, Qt.AlignRight)  # 添加坐标，靠左

        self.series.setName('总人口')
        self.series.setPen(QPen(QColor('red'), 1, Qt.SolidLine))    # 曲线的显示效果
        for i in range(2, 22):
            print(i)
            self.series.append(
                int(self.ws.cell(row=3, column=i).value[:4]),
                int(self.ws.cell(row=4, column=i).value)
            )
        self.series2.setName('男性人口')
        self.series2.setPen(QPen(QColor('green'), 1, Qt.SolidLine))  # 曲线的显示效果
        for i in range(2, 22):
            print(i)
            self.series2.append(
                int(self.ws.cell(row=3, column=i).value[:4]),
                int(self.ws.cell(row=5, column=i).value)
            )
        self.series3.setName('女性人口')
        self.series3.setPen(QPen(QColor('blue'), 1, Qt.SolidLine))  # 曲线的显示效果
        for i in range(2, 22):
            print(i)
            self.series3.append(
                int(self.ws.cell(row=3, column=i).value[:4]),
                int(self.ws.cell(row=6, column=i).value)
            )

        self.chart.setTitle('曲线图示例')
        self.chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations)    # 动画
        # self.chart.legend().hide()  # 隐藏图例
        self.chart.addSeries(self.series)   # 输入数据
        self.chart.addSeries(self.series2)  # 输入数据
        self.chart.addSeries(self.series3)  # 输入数据
        self.chart.setAxisX(self.axisX, self.series)
        self.chart.setAxisX(self.axisX, self.series2)
        self.chart.setAxisX(self.axisX, self.series3)
        self.chart.setAxisY(self.axisY, self.series)
        self.chart.setAxisY(self.axisY2, self.series2)
        self.chart.setAxisY(self.axisY2, self.series3)


        layout = QHBoxLayout()
        layout.addWidget(self.chartview)
        self.setLayout(layout)
        self.chartview.setRenderHint(QPainter.Antialiasing) # 防止变形




if __name__ == "__main__":
    app = QApplication(sys.argv)
    v = ShowPage()
    v.show()
    sys.exit(app.exec_())
